﻿using Itenso.Rtf;
using Itenso.Rtf.Converter.Html;
using Itenso.Rtf.Converter.Image;
using Itenso.Rtf.Interpreter;
using Itenso.Rtf.Parser;
using Itenso.Rtf.Support;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Itenso.Solutions.Community.Rtf2Html
{
    public class RtfToHtml
    {
        public static string ConvertString(string rtfString)
        {
            // parse rtf
            IRtfGroup rtfStructure = ParseRtf(rtfString);

            // interpret rtf
            IRtfDocument rtfDocument = InterpretRtf(rtfStructure, null);

            // convert to hmtl
            string html = ConvertHmtl(rtfDocument, null);

            return html;
        }

        private static IRtfGroup ParseRtf(string rtfString)
        {
            IRtfGroup rtfStructure;
            try
            {

                // rtf parser
                // open readonly - in case of dominant locks...
                using (MemoryStream stream=new MemoryStream())
                {
                    byte[] data = System.Text.Encoding.UTF8.GetBytes(rtfString);
                    stream.Write(data,0,data.Length);
                    stream.Position = 0;
                    // parse the rtf structure
                    RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
                    RtfParser parser = new RtfParser(structureBuilder);
                    parser.IgnoreContentAfterRootGroup = true; // support WordPad documents
                    parser.Parse(new RtfSource(stream));
                    rtfStructure = structureBuilder.StructureRoot;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("error while parsing rtf: " + e.Message);
                return null;
            }

            return rtfStructure;
        } // ParseRtf

        // ----------------------------------------------------------------------
        private static IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter)
        {
            IRtfDocument rtfDocument;
            RtfInterpreterListenerFileLogger interpreterLogger = null;
            try
            {

                // image converter
                RtfImageConverter imageConverter = null;

                // rtf parser
                // interpret the rtf structure using the extractors
                rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger, imageConverter);

            }
            catch (Exception e)
            {
                if (interpreterLogger != null)
                {
                    interpreterLogger.Dispose();
                }

                Console.WriteLine("error while interpreting rtf: " + e.Message);
                return null;
            }

            return rtfDocument;
        } // InterpretRtf

        // ----------------------------------------------------------------------
        private static string ConvertHmtl(IRtfDocument rtfDocument, IRtfVisualImageAdapter imageAdapter)
        {
            string html;

            try
            {
                RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings();
                htmlConvertSettings.CharacterSet = "CS";
                htmlConvertSettings.Title = "";
                htmlConvertSettings.ImagesPath = "";
                htmlConvertSettings.IsShowHiddenText = false;
                htmlConvertSettings.UseNonBreakingSpaces = false;
                htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.All;

                htmlConvertSettings.SpecialCharsRepresentation = "SC";

                RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings);
                html = htmlConverter.Convert();
            }
            catch (Exception e)
            {
                Console.WriteLine("error while converting to html: " + e.Message);
                return null;
            }

            return html;
        } // ConvertHmtl
    }
}
